<?php

namespace App\Http\Controllers\Home;

use App\Http\Controllers\Controller;
use App\Models\Article;
use Illuminate\Http\Request;

class IndexController extends Controller {


    public function index(){
        $username = auth()->user()->username;
        return view('home.index',compact('username'));
    }

    public function report(){
        try {
            $uid = auth('web')->id();
            $params['uid'] = $uid;
            $lists = Article::list($params);
            $this->stateToWord($lists, [
                'street' => Article::STREET_TEXT,
                'special' => Article::SPECIAL_TEXT,
                'nature' => Article::NATURE_TEXT,
                'form' => Article::FORM_TEXT,
                'sfpe' => Article::SFPE_TEXT,
                'maintenance' => Article::MAINTENANCE_TEXT,
                'fund' => Article::FUND_TEXT,
            ]);
            $lists = json_encode($lists);
            return view('home.report',compact('lists'));
        } catch (\Throwable $e) {
            return $this->returnJson(1, null, '响应失败，请联系相关人员');
        }
    }

    public function write(Request $request){
        try {
            $id = $request->get('id', 0);
            $info = [
                'id' => 0, 'street' => '', 'community' => '', 'village' => '', 'special' => '', 'building' => '', 'name' => '',
                'address' => '', 'thumb' => [], 'area' => '', 'ground' => '', 'layer' => '', 'underground' => '', 'height' => '',
                'nature' => '', 'shape' => [], 'year' => '', 'form' => '', 'body' => '', 'person' => '', 'sfpe' => '', 'mobile' => '',
                'maintenance' => '', 'company' => '', 'fund' => '', 'rainshed' => '', 'rainshed_thumb' => [], 'wall' => '', 'wall_thumb' => [],
                'bicycle' => '', 'bicycle_thumb' => [], 'sign' => '', 'sign_thumb' => [], 'channel' => '', 'channel_thumb' => [], 'speak' => '',
                'speak_thumb' => [], 'fire' => '', 'fire_use' => '', 'fire_use_thumb' => [], 'hydrant' => '', 'hydrant_use' => '', 'hydrant_use_thumb' => [],
                'whydrant' => '', 'whydrant_use' => '', 'whydrant_use_thumb' => [],'outfire' => '', 'outfire_use' => '', 'outfire_use_thumb' => [],
                'smoke' => '', 'smoke_use' => '', 'smoke_use_thumb' => [], 'shutter' => '', 'shutter_use' => '', 'shutter_use_thumb' => [],
                'gas' => '', 'gas_use' => '', 'gas_use_thumb' => [], 'keynote' => '', 'warm' => '', 'hidden1' => '', 'hidden1_thumb' => [],
                'hidden2' => '', 'hidden2_thumb' => [], 'hidden3' => '', 'hidden3_thumb' => [], 'hidden4' => '', 'hidden4_thumb' => [],
                'hidden5' => '', 'hidden5_thumb' => [], 'hidden6' => '', 'hidden6_thumb' => [], 'hidden7' => '', 'hidden7_thumb' => [],
                'hidden8' => '', 'hidden8_thumb' => [], 'hidden9' => '', 'hidden9_thumb' => [], 'hidden10' => '', 'hidden10_thumb' => [],
                'hidden11' => '', 'hidden11_thumb' => [], 'hidden12' => '', 'hidden12_thumb' => [], 'hidden13' => '', 'hidden13_thumb' => [],
                'hidden14' => '', 'hidden14_thumb' => [], 'p_address' => '', 'p_person' => '', 'p_time' => ''
            ];
            if($id){
                $info = Article::where('uid',auth('web')->id())->findOrFail($id);
            }
            $info = json_encode($info);
            $streets = json_encode(Article::STREET_TEXT);
            $specials = json_encode(Article::SPECIAL_TEXT);
            $natures = json_encode(Article::NATURE_TEXT);
            $shapes = json_encode(Article::SHAPE_TEXT);
            $forms = json_encode(Article::FORM_TEXT);
            $sfpes = json_encode(Article::SFPE_TEXT);
            $maintenances = json_encode(Article::MAINTENANCE_TEXT);
            $funds = json_encode(Article::FUND_TEXT);
            $rainsheds = json_encode(Article::EAINSHED_TEXT);
            $walls = json_encode(Article::WALL_TEXT);
            $bicycles = json_encode(Article::BICYCLE_TEXT);
            $signs = json_encode(Article::SIGN_TEXT);
            $channels = json_encode(Article::CHANNEL_TEXT);
            $speaks = json_encode(Article::SPEAK_TEXT);
            $fires = json_encode(Article::FIRE_TEXT);
            $fire_uses = json_encode(Article::FIRE_USE_TEXT);
            $hydrants = json_encode(Article::HYDRANT_TEXT);
            $hydrant_uses = json_encode(Article::HYDRANT_USE_TEXT);
            $whydrants = json_encode(Article::WHYDRANT_TEXT);
            $whydrant_uses = json_encode(Article::WHYDRANT_USE_TEXT);
            $outfires = json_encode(Article::OUTFIRE_TEXT);
            $outfire_uses = json_encode(Article::OUTFIRE_USE_TEXT);
            $smokes = json_encode(Article::SMOKE_TEXT);
            $smoke_uses = json_encode(Article::SMOKE_USE_TEXT);
            $shutters = json_encode(Article::SHUTTER_TEXT);
            $shutter_uses = json_encode(Article::SHUTTER_USE_TEXT);
            $gass = json_encode(Article::GAS_TEXT);
            $gas_uses = json_encode(Article::GAS_USE_TEXT);
            $keynotes = json_encode(Article::KEYNOTE_TEXT);
            $warms = json_encode(Article::WARM_TEXT);
            $hidden1s = json_encode(Article::HIDDEN1_TEXT);
            $hidden2s = json_encode(Article::HIDDEN2_TEXT);
            $hidden3s = json_encode(Article::HIDDEN3_TEXT);
            $hidden4s = json_encode(Article::HIDDEN4_TEXT);
            $hidden5s = json_encode(Article::HIDDEN5_TEXT);
            $hidden6s = json_encode(Article::HIDDEN6_TEXT);
            $hidden7s = json_encode(Article::HIDDEN7_TEXT);
            $hidden8s = json_encode(Article::HIDDEN8_TEXT);
            $hidden9s = json_encode(Article::HIDDEN9_TEXT);
            $hidden10s = json_encode(Article::HIDDEN10_TEXT);
            $hidden11s = json_encode(Article::HIDDEN11_TEXT);
            $hidden12s = json_encode(Article::HIDDEN12_TEXT);
            $hidden13s = json_encode(Article::HIDDEN13_TEXT);
            $hidden14s = json_encode(Article::HIDDEN14_TEXT);

            return view('home.write',compact('info','streets', 'specials', 'natures' , 'shapes',
                'forms', 'sfpes', 'maintenances', 'funds', 'rainsheds', 'walls', 'bicycles', 'signs', 'channels', 'speaks', 'fires', 'fire_uses',
                'hydrants', 'hydrant_uses', 'whydrants', 'whydrant_uses', 'outfires', 'outfire_uses', 'smokes', 'smoke_uses',
                'shutters', 'shutter_uses', 'gass', 'gas_uses', 'keynotes', 'warms', 'hidden1s', 'hidden2s', 'hidden3s', 'hidden4s',
                'hidden5s', 'hidden6s', 'hidden7s', 'hidden8s', 'hidden9s', 'hidden10s', 'hidden11s', 'hidden12s', 'hidden13s', 'hidden14s',
            ));
        } catch (\Throwable $e) {
            return $this->returnJson(1, null, '响应失败，请联系相关人员');
        }
    }

    public function update(Request $request){
        try {
            $data = $request->all();
            if (isset($data['id']) && $data['id']) {
                $info = Article::findOrFail($data['id']);
                $info->update($data);
                return $this->returnJson(0, null, '修改信息成功');
            } else {
                $data['uid'] = auth('web')->id();
                Article::create($data);
                return $this->returnJson(0, null, '提交信息成功');
            }
        } catch (\Throwable $e) {
            return $this->returnJson(1, null, '提交失败，请联系相关人员');
        }
    }
}
